﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using PlayDeezGames.Common;
using System.Xml.Linq;

namespace DungeonDelver.Engine
{
    public class Atlas : DescriptoredEntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, System.Xml.Linq.XElement theXElement)
            {
                return new Atlas(theParent as Game, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new Atlas(theParent as Game);
            }
            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.AtlasFactory, new Atlas.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.AtlasFactory);
            }
        }

        private Atlas(Game parent)
            : base(parent)
        {
        }

        private Atlas(Game parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }
        public Game Game
        {
            get
            {
                return ParentEntity as Game;
            }
        }

        private Dictionary<int, AtlasColumn> atlasColumns = new Dictionary<int, AtlasColumn>();

        public Dictionary<int, AtlasColumn>.KeyCollection Columns
        {
            get
            {
                return atlasColumns.Keys;
            }
        }

        public AtlasColumn this[int index]
        {
            get
            {
                if (atlasColumns.ContainsKey(index))
                {
                    return atlasColumns[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (atlasColumns.ContainsKey(index))
                {
                    atlasColumns[index].ParentEntity=null;
                    atlasColumns.Remove(index);
                }
                if (value != null)
                {
                    atlasColumns[index] = value;
                }
            }
        }



        protected override System.Xml.Linq.XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.Atlas);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.AtlasFactory));
            return result;
        }

        protected override void OnFromXElement(System.Xml.Linq.XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            foreach (var child in Children)
            {
            }
        }

        public static Atlas Create(Game theGame)
        {
            return EntityFactories.Create(theGame, Names.EngineFactories.AtlasFactory) as Atlas;
        }

        public static Atlas Load(Game theGame, XElement theXElement)
        {
            return EntityFactories.Load(theGame, theXElement) as Atlas;
        }
    }
}
